package com.qgxy.spsave;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.qgxy.spsave.utils.SPSaveAccountPassword;

import java.util.Map;

public class LoginActivity extends AppCompatActivity {

    private EditText etAccount, etPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_login);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.login), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        Button btnLogin = findViewById(R.id.btn_login);
        etAccount = findViewById(R.id.et_account);
        etPassword = findViewById(R.id.et_password);

        Map<String, String> userInfoMap = SPSaveAccountPassword.getAccountPassword(this);

        etAccount.setText(userInfoMap.get("account"));
        etPassword.setText(userInfoMap.get("password"));

        btnLogin.setOnClickListener(v -> {
            boolean b = SPSaveAccountPassword.saveAccountPassword(
                    this,
                    etAccount.getText().toString().trim(),
                    etPassword.getText().toString().trim()
            );
            if (b){
                Toast.makeText(this, "存储成功", Toast.LENGTH_LONG).show();
            }else {
                Toast.makeText(this, "存储失败", Toast.LENGTH_LONG).show();
            }
        });

    }
}